package com.yyq.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyq.eduservice.entity.EduCourse;
import com.yyq.eduservice.entity.vo.CourseInfo;
import com.yyq.eduservice.entity.vo.CourseInfoPublish;
import com.yyq.eduservice.entity.vo.CourseQuery;
import com.yyq.eduservice.service.EduCourseService;
import com.yyq.result.R;
import com.yyq.servicebase.exceptionhandler.YyqException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author yyq
 * @since 2021-04-15
 */
@CrossOrigin //跨域
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;
    //根据课程名称获取课程id
    @GetMapping("getCourseIdByName/{name}")
    public String getCourseIdByName(@PathVariable String name){
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        wrapper.eq("title",name);
        EduCourse one = eduCourseService.getOne(wrapper);
        if(one==null) throw new YyqException(20001,"请输入正确的课程名称");
        return one.getId();
    }
    //添加课程信息　其中包括两张表 course and course_descreption
    @PostMapping("addCourse")
    private R addcourse(@RequestBody  CourseInfo courseInfo){  //切记加注解@RequestBody以json传数据
        //定义一个实体类包含提交信息
        String id=eduCourseService.addcourse(courseInfo);
        return R.ok().data("id",id);
    }

    @GetMapping("getCourseInfoById/{courseId}")
    public  R getCourseInfoById(@PathVariable String courseId){
        CourseInfo courseInfo=new CourseInfo();
        courseInfo=eduCourseService.getCourseInfoById(courseId);
        return R.ok().data("info",courseInfo);
    }

    @PostMapping("updateCourseInfo")
    public  R updateCourseInfo(@RequestBody CourseInfo courseInfo){
        eduCourseService.updateCourseInfo(courseInfo);
        return R.ok();
    }
    //获取发布信息
    @GetMapping("getCourseInfoPublish/{id}")
    public R getCourseInfoPublish(@PathVariable String id){
        CourseInfoPublish courseInfoPublish =new CourseInfoPublish();
        courseInfoPublish=eduCourseService.publisCourseInfo(id);
        return R.ok().data("info",courseInfoPublish);
    }
    //确认发布　修改数据库　status Draft ->  Nomal
    @PostMapping("publishCourseFin/{id}")
        public R publishFin(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return  R.ok();
    }
    //分页查询
    //current 当前页 size　页面
    @GetMapping("pageList/{current}/{size}")
    public  R pageListQuery(@PathVariable long current,@PathVariable long size){
        Page<EduCourse> pages=new Page<>(current,size);
        eduCourseService.page(pages, null);
        return  R.ok().data("total",pages.getTotal()).data("list",pages.getRecords());
    }
    //多条件分页查询
    @PostMapping("pageListTJ/{current}/{size}")
    public R pageListQueryTJ(@PathVariable long current, @PathVariable long size, @RequestBody CourseQuery courseQuery){
        Page<EduCourse> pages=new Page<>(current,size);
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        String title=courseQuery.getTitle();
        String status=courseQuery.getStatus();
        if(title!=null) wrapper.like("title",title);
        if(status!=null) wrapper.eq("status",status);
        eduCourseService.page(pages,wrapper);
        return R.ok().data("total",pages.getTotal()).data("list",pages.getRecords());
    }

    @DeleteMapping("deleteAllCourseInfoByID/{courseId}")
    public R deleteAllInfo(@PathVariable String courseId){
        eduCourseService.deleteAllInfo(courseId);
        return R.ok();
    }




}

